// 表单数据
const exerciseForm = ref({
  exerciseTypeId: null,
  startTime: formatDateToYMDHMS(new Date()),
  endTime: null,
  durationMinutes: 30,
  intensity: 'MEDIUM',
  location: '',
  notes: '',
  caloriesBurned: 0
})

// 表单验证规则
const rules = {
  exerciseTypeId: [
    { required: true, message: '请选择运动类型', trigger: 'change' },
    { type: 'number', message: '运动类型ID必须是数字', trigger: 'change' }
  ],
  startTime: [
    { required: true, message: '请选择开始时间', trigger: 'change' }
  ],
  endTime: [
    { required: true, message: '请选择结束时间', trigger: 'change' }
  ],
  durationMinutes: [
    { required: true, message: '请输入持续时间', trigger: 'blur' },
    { type: 'number', min: 1, max: 1440, message: '持续时间范围在1-1440分钟之间', trigger: 'blur' }
  ],
  intensity: [
    { required: true, message: '请选择运动强度', trigger: 'change' }
  ]
}

async submitForm() {
  if (!formRef.value) return
  
  await formRef.value.validate(async (valid) => {
    if (!valid) {
      ElMessage.warning('请检查表单是否填写正确')
      return
    }
    
    if (!exerciseForm.value.exerciseTypeId) {
      ElMessage.warning('请选择运动类型')
      return
    }

    // 验证数据范围
    if (exerciseForm.value.durationMinutes > 1440) {
      exerciseForm.value.durationMinutes = 1440
      ElMessage.warning('运动时长已限制为24小时(1440分钟)')
    }

    if (exerciseForm.value.caloriesBurned > 10000) {
      exerciseForm.value.caloriesBurned = 10000
      ElMessage.warning('卡路里消耗已限制为10000卡路里')
    }

    loading.value = true
    try {
      // 构建提交数据
      const submitData = {
        exerciseTypeId: Number(exerciseForm.value.exerciseTypeId),
        startTime: exerciseForm.value.startTime,
        endTime: exerciseForm.value.endTime,
        durationMinutes: Number(exerciseForm.value.durationMinutes),
        intensity: exerciseForm.value.intensity,
        location: exerciseForm.value.location || "",
        notes: exerciseForm.value.notes || "",
        caloriesBurned: Number(exerciseForm.value.caloriesBurned)
      }
      
      console.log('提交数据:', {
        表单数据: exerciseForm.value,
        处理后数据: submitData
      })
      
      let response
      if (isEdit.value) {
        response = await updateExerciseRecord(route.params.id, submitData)
        console.log('更新运动记录成功:', response)
        ElMessage.success('运动记录更新成功')
      } else {
        response = await createExerciseRecord(submitData)
        console.log('创建运动记录成功:', response)
        ElMessage.success('运动记录添加成功')
      }
      
      goBack()
    } catch (error) {
      console.error('表单提交失败:', {
        错误类型: error.name,
        错误消息: error.message,
        响应状态: error.response?.status,
        响应数据: error.response?.data
      })
      
      // 尝试获取详细错误消息
      let errorMessage = '操作失败，请重试'
      if (error.response?.data?.message) {
        errorMessage = error.response.data.message
      } else if (error.response?.data?.error) {
        errorMessage = error.response.data.error
      } else if (error.message) {
        errorMessage = error.message
      }
      
      ElMessage.error(errorMessage)
    } finally {
      loading.value = false
    }
  })
}

 